package org.groupg.project.shell;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;

public class GroovyShellWithBindingExample {
    public static void main(String[] args) {

        Binding binding = new Binding();
        GroovyShell shell = new GroovyShell(binding);

        // 创建 Binding 实例，并设置变量
        binding.setVariable("location", "San Francisco");

        // Groovy 脚本作为字符串
        String script = "def greet(name) { return \"Hello, $name from $location!\" }\n" +
                        "greet('Java')";

        try {
            // 执行脚本并获取结果，同时传入 Binding
            Object result = shell.evaluate(script);

            // 输出结果
            System.out.println("Result from Groovy: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}